package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.codetable.CtCostumeCategory;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductWorkFlow;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class ProductWorkFlowServiceImpl extends AbstractBaseService {
	
	public final static String COLUMN_PI_ID = "piId";
	
	public final static String COLUMN_IS_DESIGN_CHART_FINISHED = "isDesignChartFinished";
	
	public final static String COLUMN_IS_CONFIRMED_BY_FACTORY = "isConfirmedByFactory";
	
	public final static String COLUMN_IS_CONFIRMED_BY_CUSTOMER = "isConfirmedByCustomer";
	
	public final static String COLUMN_IS_PRICED = "isPriced";
	
	public final static String COLUMN_IS_CONFIRMED_BY_DZ = "isConfirmedByDz";
	
	public final static String COLUMN_IS_PROCUREMENT_FINISHED = "isProcurementFinished";
	
	public final static String COLUMN_IS_PRODUCE_FINISHED = "isProduceFinished";
	
	public final static String COLUMN_IS_PACKAGE_FINISHED = "isPackageFinished";
	
	public final static String COLUMN_IS_ALL_FINISHED = "isAllFinished";
	
	
	private ProductIdentificationServiceImpl piServiceImpt;
	
	private CtServiceImpl ctService;
	
	public ProductWorkFlowServiceImpl(){
		super();
		this.piServiceImpt = new ProductIdentificationServiceImpl();
		this.ctService = new CtServiceImpl();
	}
	@Override
	public String delete(int id) throws Exception {
		return super.delete(ProductWorkFlow.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductWorkFlow obj = (ProductWorkFlow)this.dao.get(ProductWorkFlow.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		
		xmlSB.append("<" + COLUMN_PI_ID + ">");
		xmlSB.append(obj.getProductIdentification().getId());
		xmlSB.append("</" + COLUMN_PI_ID + ">");
		xmlSB.append(this.piServiceImpt.toXMLFragment(obj.getProductIdentification()));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filter) throws Exception {
		this.piServiceImpt.setLocale(this.locale);
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<ProductWorkFlow> objList = this.dao.getAllPagination(ProductWorkFlow.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductWorkFlow obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			
			xmlSB.append("<" + COLUMN_PI_ID + ">");
			xmlSB.append(obj.getProductIdentification().getId());
			xmlSB.append("</" + COLUMN_PI_ID + ">");
			
			xmlSB.append(this.piServiceImpt.toXMLFragment(obj.getProductIdentification()));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductWorkFlow.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAll(Integer piId) throws Exception{
		return null;
	}	
	
	public ProductWorkFlow getDefault(){
		ProductWorkFlow pwf = new ProductWorkFlow();
		pwf.setIsAllFinished("02");
		pwf.setIsConfirmedByCustomer("02");
		pwf.setIsConfirmedByDz("02");
		pwf.setIsConfirmedByFactory("02");
		pwf.setIsDesignChartFinished("02");
		pwf.setIsPackageFinished("02");
		pwf.setIsPriced("02");
		pwf.setIsProcurementFinished("02");
		pwf.setIsProduceFinished("02");
		return pwf;
	}

	public static void main(String[] args) throws Exception{
		ProductWorkFlowServiceImpl os = new ProductWorkFlowServiceImpl();
		System.out.println(os.getAllPagination(1, "id", "asc", null));
		//char index = 'C';
		//System.out.println(++index);
	}
}



